﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace PlanningGIS.ArcGIS.Custom
{
    /// <summary>
    /// 概述：表示图层配置<br />
    /// </summary>
    [DataContract]
    public class LayerConfig
    {
        /// <summary>
        /// 获取或设置图层配置的名称。
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// 获取或设置操作集合。
        /// </summary>
        [DataMember]
        public List<Action> Actions { get; set; }

        /// <summary>
        /// 获取或设置图层集合。
        /// </summary>
        [DataMember]
        public List<Layer> Layers { get; set; }

        /// <summary>
        /// 构造函数。
        /// </summary>
        public LayerConfig()
        {
            this.Actions = new List<Action>();
            this.Layers = new List<Layer>();
        }

        /// <summary>
        /// 获取指定名称的图层。
        /// </summary>
        /// <param name="name">图层的名称。</param>
        /// <returns>指定名称的图层。</returns>
        public Layer GetLayer(string name)
        {
            foreach (var item in this.Layers)
            {
                Layer layer = GetLayer(item, name);
                if (layer != null)
                {
                    return layer;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取指定名称的图层（递归）。
        /// </summary>
        /// <param name="layer">图层，如果是图层组则进行递归。</param>
        /// <param name="name">图层的名称。</param>
        /// <returns>指定名称的图层。</returns>
        private Layer GetLayer(Layer layer, string name)
        {
            if (layer.Name.Equals(name,StringComparison.CurrentCultureIgnoreCase))
            {
                return layer;
            }

            if (layer is GroupLayer)
            {
                foreach (var item in (layer as GroupLayer).Layers)
                {
                    Layer lyr = GetLayer(item, name);
                    if (lyr != null)
                    {
                        return lyr;
                    }
                }
            }

            return null;
        }
    }
}
